package com.ruoyi.project.article.service.impl;

import java.util.List;
import javax.annotation.PostConstruct;
import org.quartz.JobDataMap;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.constant.ScheduleConstants;
import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.common.utils.job.CronUtils;
import com.ruoyi.common.utils.job.ScheduleUtils;
//import com.ruoyi.project.article.controller.articleIds;
import com.ruoyi.project.article.domain.Article;
import com.ruoyi.project.article.mapper.ArticleMapper;
import com.ruoyi.project.article.service.IArticleService;

/**
 * 文章调度信息 服务层
 * 
 * @author ruoyi
 */
@Service
public class ArticleServiceImpl implements IArticleService
{
//    @Autowired
//    private Scheduler scheduler;

    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 项目启动时，初始化定时器 主要是防止手动修改数据库导致未同步到文章处理（注：不能手动修改数据库ID和任务组名，否则会导致脏数据）
     
    @PostConstruct
    public void init() throws SchedulerException, TaskException
    {
//        scheduler.clear();
//        List<Article> articleList = articleMapper.selectArticleAll();
        // fxtodo
//        for (Article article : articleList)
//        {
//            ScheduleUtils.createScheduleArticle(scheduler, article);
//        }
    }
*/
    
    /**
     * 获取quartz调度器的计划任务列表
     * 
     * @param article 调度信息
     * @return
     */
    @Override
    public List<Article> selectArticleList(Article article)
    {
        return articleMapper.selectArticleList(article);
    }

    /**
     * 通过调度任务ID查询调度信息
     * 
     * @param id 文章ID
     * @return 调度任务对象信息
     */
    @Override
    public Article selectArticleById(Long id)
    {
        return articleMapper.selectArticleById(id);
    }

    
    /**
     * 暂停任务
     * 
     * @param article 调度信息
     
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int pauseArticle(Article article) throws SchedulerException
    {
        Long articleId = article.getArticleId();
        String articleGroup = article.getArticleGroup();
        article.setStatus(ScheduleConstants.Status.PAUSE.getValue());
        int rows = articleMapper.updateArticle(article);
        if (rows > 0)
        {
            scheduler.pauseArticle(ScheduleUtils.getArticleKey(articleId, articleGroup));
        }
        return rows;
    }
*/
    
    /**
     * 恢复任务
     * 
     * @param article 调度信息
     
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int resumeArticle(Article article) throws SchedulerException
    {
        Long articleId = article.getArticleId();
        String articleGroup = article.getArticleGroup();
        article.setStatus(ScheduleConstants.Status.NORMAL.getValue());
        int rows = articleMapper.updateArticle(article);
        if (rows > 0)
        {
            scheduler.resumeArticle(ScheduleUtils.getArticleKey(articleId, articleGroup));
        }
        return rows;
    }
*/
    
    /**
     * 删除任务后，所对应的trigger也将被删除
     * 
     * @param article 调度信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteArticle(Long id) throws SchedulerException
    {
        int rows = articleMapper.deleteArticleById(id);
        System.out.println("fx deleteArticle rows="+rows);
        
        return rows;
    }

    
    /**
     * 批量删除调度信息
     * 
     * @param articleIds 需要删除的任务ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteArticleByIds(Long[] articleIds) throws SchedulerException
    {
        for (Long id : articleIds)
        {
        	System.out.println("fx deleteArticleByIds id="+id);
//            Article article = articleMapper.selectArticleById(id);
            deleteArticle(id);
        }
    }

    
    /**
     * 任务调度状态修改
     * 
     * @param article 调度信息
     
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int changeStatus(Article article) throws SchedulerException
    {
        int rows = 0;
        String status = article.getStatus();
        if (ScheduleConstants.Status.NORMAL.getValue().equals(status))
        {
            rows = resumeArticle(article);
        }
        else if (ScheduleConstants.Status.PAUSE.getValue().equals(status))
        {
            rows = pauseArticle(article);
        }
        return rows;
    }
*/
    
    /**
     * 立即运行任务
     * 
     * @param article 调度信息
     
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean run(Article article) throws SchedulerException
    {
        boolean result = false;
        Long articleId = article.getArticleId();
        String articleGroup = article.getArticleGroup();
        Article properties = selectArticleById(article.getArticleId());
        // 参数
        ArticleDataMap dataMap = new ArticleDataMap();
        dataMap.put(ScheduleConstants.TASK_PROPERTIES, properties);
        ArticleKey articleKey = ScheduleUtils.getArticleKey(articleId, articleGroup);
        if (scheduler.checkExists(articleKey))
        {
            result = true;
            scheduler.triggerArticle(articleKey, dataMap);
        }
        return result;
    }
*/
    
    /**
     * 新增任务
     * 
     * @param article 调度信息 调度信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertArticle(Article article) throws SchedulerException, TaskException
    {
        article.setStatus(ScheduleConstants.Status.PAUSE.getValue());
        int rows = articleMapper.insertArticle(article);
        return rows;
    }

    
    /**
     * 更新任务的时间表达式
     * 
     * @param article 调度信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateArticle(Article article) throws SchedulerException, TaskException
    {
        int rows = articleMapper.updateArticle(article);
        System.out.println("fx article updateArticle rows="+rows);
        return rows;
    }

    
    /**
     * 更新任务
     * 
     * @param article 任务对象
     * @param articleGroup 任务组名
     
    public void updateSchedulerArticle(Article article, String articleGroup) throws SchedulerException, TaskException
    {
        Long articleId = article.getArticleId();
        // 判断是否存在
        ArticleKey articleKey = ScheduleUtils.getArticleKey(articleId, articleGroup);
        if (scheduler.checkExists(articleKey))
        {
            // 防止创建时存在数据问题 先移除，然后在执行创建操作
            scheduler.deleteArticle(articleKey);
        }
        ScheduleUtils.createScheduleArticle(scheduler, article);
    }
*/
    
    /**
     * 校验cron表达式是否有效
     * 
     * @param cronExpression 表达式
     * @return 结果
     
    @Override
    public boolean checkCronExpressionIsValid(String cronExpression)
    {
        return CronUtils.isValid(cronExpression);
    }
    */
}